package com.jiazhong.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

//创建一个比较复杂的工厂
public class Myfactory {
    private Properties properties = new Properties();

    public Myfactory() {

    }

    public Myfactory(String name) throws IOException {
        //通过反射类获取配置文件
        InputStream name1 = Myfactory.class.getResourceAsStream(name);
        //读取配置文件信息
        properties.load(name1);
    }

    public Object getBean(String config) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //获取对应类的路径
        String classpath = properties.getProperty(config);
        //2.通过反射构建对象
        Class<?> aClass = Class.forName(classpath);
        //3.通过反射调用构造器实例化对象
        return aClass.newInstance();
    }
}
